<?php
error_reporting(0);
$isadmin = 1;
include '../../comm/config.php';
include '../../comm/msgset.php';
include '../../comm/checkpostandget.php';
include '../check.php';

$id = $_GET['id'];

if ($id == "") {
	echo "错误：没有要结算的数据！";
	exit;
}

$q = "select * from " . $BIAOTOU . "mall_order where Id=" . $id;
//echo $q;
$rs = mysql_query($q);
$num = mysql_num_rows($rs);
//echo $num;
if (!$num) {
	echo "错误：未找到该记录！";
	exit;
} else {
	$sql = "select merchant_id,u_id,commission,order_code,stat_desc,fxje from " . $BIAOTOU . "mall_order where id=" . $id;
	$query = mysql_query($sql);
	$row = mysql_fetch_array($query);
	$merchant_id = $row['merchant_id'];
	$u_id = $row['u_id'];
	$commission = $row['commission'];
	$order_code = $row['order_code'];
	$stat_desc = $row['stat_desc'];
	$fxje = $row['fxje'];

	if ($u_id > 0 and $stat_desc == '核对有效') {
		$sql = "select ddusername,dengji from " . $BIAOTOU . "user where Id=" . $u_id . "";
		mysql_query("set names utf8");
		$rs = mysql_query($sql);
		//该代号的会员如果存在
		if (mysql_num_rows($rs) != 0) {
			while ($rowuser = mysql_fetch_object($rs)) {
				$ddusername = $rowuser->ddusername;
				$dddengji = $rowuser->dengji;  //取会员等级
			}

			$sj = date('Y-m-d H:i:s');

			//用户佣金
			$sql = "update " . $BIAOTOU . "user set `money`=`money`-'" . $fxje . "',dengji=dengji-1 where ddusername ='" . $ddusername . "'";
			mysql_query("set names utf8");
			mysql_query($sql);
			//用户消息
			$msg_deltrade = str_replace("[trade_id]", $order_code, $msg_deltrade);
			$msg_deltrade = str_replace("[fxje]", $fxje, $msg_deltrade);
			$sql = "insert into " . $BIAOTOU . "msg (title,content,addtime,see,ddusername) values ('一笔交易被取消！','" . $msg_deltrade . "','$sj',0,'" . $ddusername . "');";
			mysql_query("set names utf8");
			mysql_query($sql);
			//用户明细
			echo $sql = "insert into " . $BIAOTOU . "mingxi (ddusername,shijian,je,addtime,memo) values ('" . $ddusername . "','交易取消'," . - $fxje . ",'$sj','".$merchant_id."交易号" . $order_code . "')";
			mysql_query($sql);
			//取推荐人
			$sql = "select tjr from " . $BIAOTOU . "user where ddusername='" . $ddusername . "' and tjr is not NULL tjr<>''";
			mysql_query("set names utf8");
			$t = mysql_query($sql);
			$num1 = mysql_num_rows($t);
			//echo 	$sql;					
			if ($num1 != 0) {
				$tjrid = "";
				while ($tt = mysql_fetch_array($t)) {
					$tjrid = $tt["tjr"]; //推广人佣金
					$sql = "update " . $BIAOTOU . "user set money=money-" . round($commission * TGBL, 2) . " where Id='" . $tjrid . "'";
					mysql_query("set names utf8");
					mysql_query($sql);

					//取推荐人名
					$sqltjr = "select ddusername from " . $BIAOTOU . "user where Id='" . $tjrid . "'";
					$tgrq = mysql_query($sqltjr);
					while ($tu = mysql_fetch_array($tgrq)) {
						$tjrusername = $tu["ddusername"];
					}

					//推广人明细
					$sql = "insert into " . $BIAOTOU . "mingxi (ddusername,shijian,je,addtime,memo) values ('" . $tjrusername . "','取消推广佣金'," . -round($commission * TGBL, 2) . ",'$sj','交易人" . $ddusername . "，交易号" . $order_code . "')";
					mysql_query("set names utf8");
					mysql_query($sql);
					//推广人消息
					$tgje = round($commission * TGBL, 2);
					$msg_deltradetuiguang = str_replace("[ddusername]", $ddusername, $msg_deltradetuiguang);
					$msg_deltradetuiguang = str_replace("[trade_id]", $order_code, $msg_deltradetuiguang);
					$msg_deltradetuiguang = str_replace("[tgje]", $tgje, $msg_deltradetuiguang);
					$sql = "insert into " . $BIAOTOU . "msg (title,content,addtime,see,ddusername) values ('取消推广佣金！','" . $msg_deltradetuiguang . "','$sj',0,'" . $tjrusername . "');";
					mysql_query("set names utf8");
					mysql_query($sql);
				}
			}
		}
	}

	$q = "delete from " . $BIAOTOU . "mall_order where id=" . $id;
	mysql_query($q);
}
//header('Location: mallorders.php');     
admin_log('删除商城订单');
mysql_close();
?>
<script>
alert('删除成功！');history.go(-1);
</script>